<template>
	<view class="container">
		<!-- 顶部返回与编辑 -->
		<view class="header">
			<!-- <view class="back" @click="goBack">←</view> -->
			<!-- <view class="title">骏山项目</view> -->
			<view class="title">{{ projectName }}</view>
			<!-- 			<view class="edit">
				<u-icon name="edit-pen" size="28"></u-icon>
			</view> -->
			<view class="icons">
				<image src="https://yun.qiongtuyule.com/static/edit.png" class="icon" @tap="onEdit" />
			</view>
		</view>

		<!-- 设备卡片列表 -->
		<view class="device-list">
			<view v-for="device in devices" :key="device.code" class="device-card" :class="{
					normal: device.status === '正常',
					offline: device.status === '离线',
					error: device.status === '异常'
				}" @click="goToDetail(device.code)">
				<view class="code">{{ device.code }}</view>
				<view class="code">{{ device.pn }}</view>
				<u-icon :name="device.status === '离线' ? 'unlink' : 'link'" size="40"
					:color="device.status === '正常' ? '#00FF00' : '#FFFFFF'"></u-icon>



				<!-- 电量与信号 -->


				<view class="info_container">
					<view class="info" :class="{ green: device.status === '正常' }">
						{{device.status}}
					</view>
					<template v-if="device.status === '正常'">
						<view class="info green">电量: {{ device.battery }}%</view>
						<view class="info green">信号: {{ device.signal_strength }}%</view>
					</template>
				</view>


				<view class="arrow" :class="{ light: device.status === '异常' }">

					<m-icon type="arrowright" size="18" color="#bbb" class="my-menu-arrow"></m-icon>

				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				projectId: null,
				devices: [],
				projectName: '',
			};
		},
		onLoad(options) {
			this.projectId = options.id;
			this.fetchDeviceList();
			this.fetchProjectInfo();
		},
		methods: {
			onEdit() {

			},
			goBack() {
				uni.navigateBack();
			},
			goToDetail(code) {
				uni.navigateTo({
					url: `/pages/deviceDetail/index?code=${code}`,
				});
			},
			async fetchDeviceList() {
				const res = await this.$lib.$http.post({
					url: this.$lib.$urlMap.deviceList,
					data: {
						page: 1,
						limit: 10,
						project_id: this.projectId
					},
					needLogin: true
				})

				if (res.code === 1) {
					this.devices = res.data
				} else {
					uni.showToast({
						title: '设备列表加载失败',
						icon: 'none'
					})
				}
			},
			async fetchProjectInfo() {
				const res = await this.$lib.$http.post({
					url: this.$lib.$urlMap.projectInfo,
					data: {
						id: this.projectId
					},
					needLogin: true
				});

				if (res.code === 1 && res.data) {
					this.projectName = res.data.name;
				} else {
					uni.showToast({
						title: '项目名称加载失败',
						icon: 'none'
					});
				}
			},
		},
	};
</script>

<style scoped>
	.info_container {
		display: flex;
		/* 启用 Flexbox 布局 */
		justify-content: center;
		/* 子元素从左侧开始排列 */
		align-items: center;
		/* 垂直居中对齐 */
		margin-top: 60rpx;
	}

	.info {
		margin-right: 10px;
		font-size: 16px;
		/* 设置字体大小 */
		line-height: 24px;
		/* 设置行高 */
		white-space: nowrap;
		/* 防止内容换行 */
	}

	.container {
		background-color: #fff;
		min-height: 100vh;
		padding: 20rpx;
	}

	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 30rpx;
		margin-top: 20rpx;
	}

	.back {
		font-size: 40rpx;
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
	}

	.edit {
		margin-right: 10rpx;
	}

	.device-list {
		display: flex;
		flex-wrap: wrap;
		gap: 20rpx;
	}

	.device-card {
		width: 300rpx;
		height: 200rpx;
		border-radius: 20rpx;
		padding: 20rpx;
		position: relative;
		color: #fff;
	}

	.device-card .code {
		font-size: 28rpx;
		margin-bottom: 10rpx;
	}

	.device-card .info {
		font-size: 24rpx;
		margin-top: 8rpx;
	}

	.device-card .info.green {
		color: #00ff00;
	}

	.device-card.normal {
		background-color: #181818;
	}

	.device-card.offline {
		background-color: #6C6C6C;
	}

	.device-card.error {
		background-color: #FF7979;
	}

	.arrow {
		position: absolute;
		top: 20rpx;
		right: 20rpx;
		font-size: 32rpx;
		color: #fff;
	}

	.arrow.light {
		color: #ffdede;
	}

	.icons {
		display: flex;
	}

	.icon {
		width: 40rpx;
		height: 40rpx;
		margin-left: 20rpx;
	}
</style>